import React, { Component } from "react";
// Prompt用于保护路由跳转的
// 有两个属性when, message
import { Link, Route, Switch, Prompt } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>首页</div>;
  }
}
class About extends Component {
  state = {
    textValue: "",
  };

  handleChange = (e) => {
    this.setState({
      textValue: e.target.value,
    });
  };

  render() {
    return (
      <div>
        <Prompt
          when={this.state.textValue}
          message={(location) =>
            `Are you sure you want to go to ${location.pathname}`
          }
        />
        <input
          size="50"
          placeholder="type something to block transitions"
          value={this.state.textValue}
          onChange={this.handleChange}
        />
      </div>
    );
  }
}
class Mine extends Component {
  render() {
    return <div>我的页</div>;
  }
}

class Basic extends Component {
  state = {};
  render() {
    return (
      <>
        <h3>路由保护</h3>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
        </ul>

        <Switch>
          <Route path="/home" component={Home} exact></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default Basic;
